<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>海量短剧搜索</title>
    <style>
        /* 全局样式，背景毛玻璃效果 https://api.mtyqx.cn/tapi/random.php*/
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: url('https://api.img.mtyqx.cn/tapi/large/0072Vf1pgy1foxkil4o6qj31hc0u0nbz.jpg') no-repeat center center fixed;
            background-size: cover;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            backdrop-filter: blur(0px);
        }

        /* 容器样式 */
        .container {
            background: rgb(255 255 255 / 65%);
			padding: 20px;
			border-radius: 15px;
			box-shadow: 0 0 25px rgb(0 0 0 / 82%);
			text-align: center;
			width: 80%;
			max-width: 1200px;
        }

        /* 标题样式 */
        h1 {
            color: #2d62ff;
            font-size: 36px;
            margin-bottom: 20px;
        }

        /* 搜索框样式 */
        .search-box {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }

        .search-input {
            padding: 10px;
            font-size: 18px;
            width: 60%;
            border-radius: 30px;
            border: none;
            margin-right: 10px;
            outline: none;
            background: rgb(16 219 181 / 38%);
        }

        .search-button {
            padding: 10px 20px;
            font-size: 18px;
            border: none;
            background-color: #007BFF;
            color: white;
            border-radius: 30px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .search-button:hover {
            background-color: #0056b3;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        table, th, td {
            border: 1px solid rgba(255, 255, 255, 0.5);
        }

        th, td {
            padding: 10px;
            text-align: left;
        }

        th {
            color: #00ff74d4;
            background-color: rgba(0, 0, 0, 0.6);
        }

        td {
			color: #06fdfffa;
            background-color: rgba(0, 0, 0, 0.4);
        }

        td a {
            color: #9bff00;
            text-decoration: none;
        }

        td a:hover {
            text-decoration: underline;
        }
        .center-text {
            text-align: center;
        }

        /* 翻页样式 */
        .pagination {
            margin-top: 20px;
            display: flex;
            justify-content: center;
            gap: 10px;
        }

        .page-button {
            padding: 10px 15px;
            font-size: 16px;
            border-radius: 5px;
            border: none;
            background-color: #007BFF;
            color: white;
            cursor: pointer;
        }

        .page-button:hover {
            background-color: #0056b3;
        }

        .page-button.disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>海量短剧 搜索</h1>

        <!-- 搜索框 -->
        <div class="search-box">
            <input id="search-input" class="search-input" type="text" placeholder="输入关键词..." />
            <button class="search-button" id="search-button">搜索一下</button>
        </div>

        <!-- 搜索结果表格 -->
        <table id="result-table">
			<colgroup>
				<col style="width: 35%">
				<col style="width: 55%">
				<col style="width: 10%">
			</colgroup>
            <thead>
                <tr>
                    <th>名称</th>
                    <th>链接</th>
                    <th class="center-text">提取码</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>

        <!-- 翻页按钮 -->
        <div id="pagination" class="pagination">
            <button id="prevPage" class="page-button disabled">上一页</button>
            <button id="nextPage" class="page-button" >下一页</button>
        </div>
    </div>

        <script>
        const searchBtn = document.getElementById("search-button");
        const prevBtn = document.getElementById("prevPage");
        const nextBtn = document.getElementById("nextPage");
        const resultTable = document.getElementById("result-table").getElementsByTagName('tbody')[0];
        const searchInput = document.getElementById("search-input");

        let data = [];
        let currentPage = 1;
        const resultsPerPage = 10;

        // 发送搜索请求
        function fetchData(query) {
            fetch(`https://v2.xxapi.cn/api/duanjusearch?search=${query}`)
                .then(response => response.json())
                .then(responseData => {
                    if (responseData.code === 200) {
                        data = responseData.data;
                        renderTable();
                        updatePagination();
                    }
                })
                .catch(error => console.error("请求失败:", error));
        }

        // 渲染表格数据
        function renderTable() {
            resultTable.innerHTML = ''; // 清空表格
            const startIndex = (currentPage - 1) * resultsPerPage;
            const endIndex = Math.min(startIndex + resultsPerPage, data.length);
            const pageData = data.slice(startIndex, endIndex);

            pageData.forEach(item => {
                const row = resultTable.insertRow();
                const titleCell = row.insertCell(0); // 名称
                const urlCell = row.insertCell(1);	// 链接
				const codeCell = row.insertCell(2); // 提取码

				titleCell.textContent = item.title;

				const link = document.createElement("a");
				// 使用正则表达式提取URL
				const urlPattern = /(https?:\/\/[^\s]+)/g; // 正则表达式匹配http或https开头的URL
				const urls = item.url.match(urlPattern); // 获取所有匹配的URL
				if (urls && urls.length > 0) {
					// 只取第一个URL（如果有多个的话）
					link.href = urls[0];
					link.target = "_blank";
					link.textContent = urls[0]; // 设置链接文本为URL
					link.title = "点击即可访问"; // 鼠标悬停时显示提示文本
					urlCell.appendChild(link);
				}

				// 提取提取码，支持两种格式，并处理换行符
				let codeText = item.url.match(/(?:密码|提取码)[\s:：]*([\w]+)/i); // 处理密码和提取码格式，支持换行符
				if (codeText && codeText.length > 1) {
					const code = codeText[1]; // 获取提取码

					// 创建提取码文本
					const codeLink = document.createElement("a");
					codeLink.href = "javascript:void(0);";
					codeLink.textContent = code;
					codeLink.title = "点击复制提取码";

					// 添加点击事件，点击后将提取码复制到剪贴板
					codeLink.addEventListener("click", () => {
						navigator.clipboard.writeText(code).then(() => {
							alert("提取码已复制到剪贴板！");
						}).catch(err => {
							alert("复制失败，请手动复制提取码。");
						});
					});

					codeCell.className="center-text"
					codeCell.appendChild(codeLink); // 将提取码添加到新的列中
				}
            });
        }

        // 更新翻页按钮状态
        function updatePagination() {
            prevBtn.disabled = currentPage === 1;
            nextBtn.disabled = currentPage * resultsPerPage >= data.length;
        }

        // 下一页按钮事件
        nextBtn.addEventListener("click", () => {
            if (currentPage * resultsPerPage < data.length) {
                currentPage++;
                renderTable();
                updatePagination();
            }
        });

        // 上一页按钮事件
        prevBtn.addEventListener("click", () => {
            if (currentPage > 1) {
                currentPage--;
                renderTable();
                updatePagination();
            }
        });

        // 搜索按钮点击事件
        searchBtn.addEventListener("click", () => {
            const query = searchInput.value.trim();
            if (query) {
                currentPage = 1; // 重置页面
                fetchData(query);
            }
        });

    </script>
</body>
</html>
